package com.test.uiapp2.function.Jingnote;

import android.util.Log;

import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;

import com.test.uiapp2.base.TestBase;
import com.test.uiapp2.element.Base.BaseElement;
import com.test.uiapp2.element.Jingnote.JingnoteElement;
import com.test.uiapp2.tools.ShellUtils;

import org.junit.Test;

import java.util.Random;

public class JingnoteFunction extends TestBase {


    //新建记事本
    public void JingnoteNewNotebook(){
        try {
            startApp(JingnoteElement.NOTE);
            sleep(WAIT);
            UiObject btn_create = getId(BaseElement.JINGNOTE, JingnoteElement.BTN_CREATE);
            if (btn_create.exists()){
                clickById(BaseElement.JINGNOTE,JingnoteElement.BTN_CREATE);
                verify("未显示新建记事本弹窗",getTest(JingnoteElement.CREATE_NOTE).exists());
                sleep(WAIT);
                UiObject NotepadInput  = getId(BaseElement.JINGNOTE, JingnoteElement.ET);
                Random random = new Random();
                int i = random.nextInt(4);
                Log.i(TAG, "随机数="+i);
                switch (i){
                    case 0:
                        NotepadInput.setText(JingnoteElement.NOTE1);
                        break;
                    case 1:
                        NotepadInput.setText(JingnoteElement.NOTE2);
                        break;
                    case 2:
                        NotepadInput.setText(JingnoteElement.NOTE3);
                        break;
                    case 3:
                        NotepadInput.setText(JingnoteElement.NOTE4);
                        break;
                }
                sleep(WAIT);
                selectcover();
                create();
            }else{
                verify("未进入新建记事本界面",btn_create.exists());
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    //选择封面方法
    public void selectcover(){
        try {
            UiObject className = getClassName(JingnoteElement.LINEARLAYOUT);
            Random random = new Random();
            int i = random.nextInt(4);
            switch (i){
                case 0:
                    UiObject child = className.getChild(new UiSelector().className(JingnoteElement.RELATIVELAYOUT).index(0));
                    child.click();
                    break;
                case 1:
                    UiObject child2 = className.getChild(new UiSelector().className(JingnoteElement.RELATIVELAYOUT).index(2));
                    child2.click();
                    break;
                case 2:
                    UiObject child4 = className.getChild(new UiSelector().className(JingnoteElement.RELATIVELAYOUT).index(4));
                    child4.click();
                    break;
                case 3:
                    UiObject child6 = className.getChild(new UiSelector().className(JingnoteElement.RELATIVELAYOUT).index(6));
                    child6.click();
                    break;
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void tcAlert(){
        //判断是否有同意弹窗
        try {
            UiObject test = getTest(JingnoteElement.TY);
            if(test.exists()){
                test.click();
                sleep(WAITTIME);
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }


    //新建笔记
//    public void  Writtencontent(){
//        try {
//            startApp(JingnoteElement.NOTE);
//            tcAlert();
//            UiObject newid = getId(BaseElement.JINGNOTE,JingnoteElement.NEW);
//            Log.i(TAG, "进入方法了");
//            if(newid.exists()){
//                clickById(BaseElement.JINGNOTE,JingnoteElement.NEW);
//                sleep(WAITTIME);
//                clickById(BaseElement.JINGNOTE,JingnoteElement.IV_INPUT_MODE);
//                sleep(WAITTIME);
//                for(int i=1;i<=20;i++){
//                    write();
//                    sleep(WAIT);
//                    UiObject page_next = getId(BaseElement.JINGNOTE, JingnoteElement.TV_PAGE_NEXT);
//                    if(i==10){
//                        break;
//                    }
//                    page_next.click();
//                    sleep(HWAIT);
//                }
//                String pagetext = getId(BaseElement.JINGNOTE, JingnoteElement.INK_ITEM_PAGE_TV).getText();
//                String substring = pagetext.substring(6, pagetext.length()-1);
//                int i = Integer.parseInt(substring);
//                verify("记事本书写内容未完成",i==10);
//                sleep(WAIT);
//                back();
//                sleep(WAIT);
//                minimize();
//            }else{
//                verify("新建笔记按钮不存在",newid.exists());
//            }
//        } catch (UiObjectNotFoundException e) {
//            e.printStackTrace();
//        }
//    }
    //新建笔记
    public void  Writtencontent(){
        try {
            startApp(JingnoteElement.NOTE);
            tcAlert();
            UiObject newid = getId(BaseElement.JINGNOTE,JingnoteElement.NEW);
            if(newid.exists()){
                for(int i=0;i<=10;i++){
                    clickById(BaseElement.JINGNOTE,JingnoteElement.NEW);
                    sleep(WAITTIME);
                    clickById(BaseElement.JINGNOTE,JingnoteElement.IV_PEN_MODE);
                    sleep(WAIT);
                    write();
                    sleep(HWAIT);
                    clickById(BaseElement.JINGNOTE,JingnoteElement.JL_IV_BACK);
                }
            }else{
                verify("新建笔记按钮不存在",newid.exists());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }






    //记事本写字
    public void write() {
        uiDevice.drag(407, 528, 369, 1082, STEP);
        sleep(WAIT);
        uiDevice.drag(546,1253,792,658,STEP);
        sleep(WAIT);
        uiDevice.drag(787,1138,979,1353,STEP);

    }

    //提交
    public void create() {
        try {
            UiObject submitButton = getTextContains(JingnoteElement.CREATE);
            for(int i = 0;i < 3;i++){
                if(submitButton.exists()){
                    Log.i(TAG, "submitAnswer: 点击创建按钮");
                    submitButton.click();
                }else {
                    Log.i(TAG, "submitAnswer: 没有创建按钮");
                    break;
                }
            }
        } catch (Exception e) {
            Log.e(TAG,e.getMessage());
        }
    }




    public void Jingnote(){
        startApp(JingnoteElement.NOTE);
        UiObject newid = getId(BaseElement.JINGNOTE,JingnoteElement.NEW);
        if(newid.exists()){
            clickById(BaseElement.JINGNOTE,JingnoteElement.NEW);
        }
        sleep(WAIT);
        uiDevice.drag(314,525,245,1016,10);
        sleep(WAIT);
        uiDevice.drag(546,1253,792,658,10);
        clickback();

    }

    public void back(){
        try {
            UiObject backid = getId("com.asa.jingnote:id/iv_back");
            if(backid.exists()){
                sleep(WAIT);
                backid.click();
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }
}
